package lt.vu.mif.os.parser;
/**
* Praser specially used by virtual machine to identify program's code
* 
* @author Antanas Rudaitis
* @author Laurynas Paradauskas
* @author Algirdas Šaltupis
* 
*/
public class Parser {
	public Parser() {
	}
	/**
	 * Parse method indentifies given code and returns command's number
	 * @param code given code/command
	 * @return gives back command's number which one will be next executed
	 */
	public static int parse(String code) {
		int cmdNr = 0;
		boolean indicator = true;
		/* Mathematical */
		if (code.startsWith("ADD")) { cmdNr = 1; }
		if (code.startsWith("SUB")) { cmdNr = 2; }
		if (code.startsWith("MUL")) { cmdNr = 3; }
		if (code.startsWith("DIV")) { cmdNr = 4; }
		/* Comparison */
		if (code.startsWith("CMP")) { cmdNr = 5; }
		/* With data/stack */
		if (code.charAt(0) == 'L' && code.charAt(1) == 'D') { cmdNr = 6; }
		if (code.charAt(0) == 'P' && code.charAt(1) == 'T') { indicator = false; cmdNr = 7; }
		if (code.charAt(0) == 'P' && code.charAt(1) == 'U' &&code.charAt(2) == 'N') { indicator = false; cmdNr = 8; }
		/* Control (Jumps) */
		if (code.charAt(0) == 'J' && code.charAt(1) == 'P') { cmdNr = 9; }
		if (code.charAt(0) == 'J' && code.charAt(1) == 'E') { cmdNr = 10; }
		if (code.charAt(0) == 'J' && code.charAt(1) == 'L') { cmdNr = 11; }
		if (code.charAt(0) == 'J' && code.charAt(1) == 'G') { cmdNr = 12; }
		if (code.startsWith("HALT")) { cmdNr = 13; }
		/* INPUT/OUTPUT */
		if (code.startsWith("PRNT")) { indicator = false; cmdNr = 14; }
		if (code.charAt(0) == 'P' && indicator) { cmdNr = 15; }
		if (code.startsWith("READ")) { cmdNr = 16; }
		/* INC/DEC */
		if (code.startsWith("INC")) { cmdNr = 17; }
		if (code.startsWith("DEC")) { cmdNr = 18; }
		/* String */
		if (code.startsWith("%%")) { cmdNr = 19; }
		return cmdNr;
	}
}
